package com.xltq.controller;

import com.xltq.entity.PageBean;
import com.xltq.entity.Product;
import com.xltq.service.IProductService;
import com.xltq.service.impl.IProductServiceImpl;
import com.xltq.utils.Constants;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.SQLException;

@WebServlet("/product")
public class ProductController extends BaseServlet {
    public String show(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //获取tid参数
        String tid = request.getParameter("tid");
        //要么自定义,要么就是前端页面回传页面数据的多少
        //通过if判断一个 约定值, 如果有前端回传,那么就是用前端的, 否则就是固定
        int pageSize = 6;//一页展示数据量

        String currentPage = request.getParameter("currentPage");
        int page = 1;//默认页
        if(currentPage != null){ //触发分页
            page = Integer.parseInt(currentPage);
        }
        //调用业务逻辑 需要向前端传递数据 PageBean
        IProductService productService = new IProductServiceImpl();
        PageBean<Product> pageBean = productService.findPage(tid, page, pageSize);

        //提供响应
        request.setAttribute("pageBean",pageBean);
        return Constants.FORWARD+"/goodsList.jsp";
    }
    public String detail(HttpServletRequest request, HttpServletResponse response)throws Exception {
        //获取tid参数
        String pid = request.getParameter("pid");
        //2调用业务逻辑
        IProductService productService = new IProductServiceImpl();
        Product product = productService.findProductByid(pid);
        request.setAttribute("product",product);
        return Constants.FORWARD+"/goodsDetail.jsp";
    }
}
